package com.bw.home.adpter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bw.home.R
import com.bw.home.databinding.ItemCommentBinding
import com.bw.home.entity.CommentEntity
import com.bw.home.viewmodel.CommentViewModel

class CommentAdapter(val context: Context, val list: List<CommentEntity>) :
    RecyclerView.Adapter<CommentAdapter.CommentHolder>() {
    class CommentHolder(val binding: ItemCommentBinding) : RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CommentHolder {
        val view = ItemCommentBinding.inflate(LayoutInflater.from(context), parent, false)
        return CommentHolder(view)
    }

    override fun onBindViewHolder(holder: CommentHolder, position: Int) {
        val entity = list.get(position)
        if (entity.userlogo.isBlank()) {
            Glide.with(context).load(R.drawable.logo).into(holder.binding.ivHeaderCommentHomeDetail)
        } else {
            Glide.with(context).load(entity.userlogo).into(holder.binding.ivHeaderCommentHomeDetail)
        }

        if (entity.nickname.isEmpty()) {
            holder.binding.tvUsernameCommentHomeDetail.text = "游客用户"
        } else {
            holder.binding.tvUsernameCommentHomeDetail.text = entity.nickname
        }

        holder.binding.tvContentCommentHomeDetail.text = entity.content

    }

    override fun getItemCount(): Int {
        return list.size
    }
}